package com.pbl6.back.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pbl6.back.domain.User;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Arrays;
import com.pbl6.back.domain.Texts;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import xin.altitude.cms.common.entity.AjaxResult;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import xin.altitude.cms.common.entity.PageEntity;
import com.pbl6.back.mapper.TextsMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import com.pbl6.back.service.ITextsService;
@RestController
@RequestMapping("/back/texts")
public class TextsController{
    @Autowired
    private ITextsService textsService;
    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity,Texts texts){
        return AjaxResult.success(textsService.page(pageEntity.toPage(), Wrappers.lambdaQuery(texts)));
    }
    @GetMapping("/list")
    public AjaxResult list(Texts texts){
        LambdaQueryWrapper<Texts> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (texts.getOperator() !=null&& !" ".equals(texts.getOperator())
                ||texts.getDeliver() !=null&& !" ".equals(texts.getDeliver())) {
//        if (texts.toString() !=null&& !" ".equals(texts.toString())){
            recordLambdaQueryWrapper.and((textsLambdaQueryWrapper) -> {
            textsLambdaQueryWrapper.like(Texts::getOperator, texts.getOperator()).or()
                                .like(Texts::getDeliver, texts.getDeliver());
                    });
        }
//            recordLambdaQueryWrapper.like(Texts::getOperator,texts.getOperator()).or();
//        }

//        if (texts.getDeliver() !=null&& !" ".equals(texts.getDeliver())) {
//            recordLambdaQueryWrapper.like(Texts::getDeliver,texts.getDeliver());
//        }
        if (texts.getMain() !=null&& !" ".equals(texts.getMain())) {
            recordLambdaQueryWrapper.like(Texts::getMain,texts.getMain());
        }
        if (texts.getIdentify() !=null&& !" ".equals(texts.getIdentify())) {
            recordLambdaQueryWrapper.like(Texts::getIdentify,texts.getIdentify());
        }
//        return AjaxResult.success(textsService.list(Wrappers.lambdaQuery(texts)));
            return AjaxResult.success(textsService.list(recordLambdaQueryWrapper));

    }
    @PostMapping("/push")
    public AjaxResult add(@RequestBody Texts texts) {
        return AjaxResult.success(textsService.save(texts));
    }
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Texts texts) {
        return AjaxResult.success(textsService.updateById(texts));
    }
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(textsService.removeByIds(Arrays.asList(ids)));
    }
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(textsService.getById(id));
    }
}
